package logic

import (
	"context"
	"fmt"
	"manage/models/mysql"

	"manage/managerpc/internal/svc"
	"manage/managerpc/managerpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LikeLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLikeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LikeLogic {
	return &LikeLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 点赞
func (l *LikeLogic) Like(in *managerpc.LikeRequest) (*managerpc.LikeResponse, error) {
	// todo: add your logic here and delete this line
	var users mysql.Like
	mysql.DB.Where("user_id = ?", in.UserId).Where("activity_id=?", in.ActivityId).Find(&users)
	if users.ID != 0 {
		return nil, fmt.Errorf("你已经赞过了")
	}
	user := mysql.Like{
		UserId:     int(in.UserId),
		ActivityId: int(in.ActivityId),
	}
	mysql.DB.Where("id = ?", in.UserId).Create(&user)
	var act mysql.Activity
	act.Liked = act.Liked + 1
	mysql.DB.Where("id=?", in.ActivityId).Updates(&act)
	return &managerpc.LikeResponse{
		Id: in.UserId,
	}, nil
}
